package util;

//import com.google.gson.Gson;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;


//import com.google.gson.Gson;

public class Codes {
	private String code;
	
	public Codes(Object code) {
		this.code = String.valueOf(code);
	}
	
	public String getCode(int nChars) {
		return Codes.getCode(code, nChars);
	}
	
	public String getCode(Object code, int nChars) {
		this.code = String.valueOf(code);
		return getCode(nChars);
	}
	
	public static String getCode(String code, int nChars) {
		String cod = code.toString();
		int i, f = nChars - cod.length();
	
		if (f > 0) {
			for (i = 0; i < f; i ++) cod = "0" + cod;
		}	
		
		return cod;
	}
	
	public static String getCode(int code, int nChars) {
		String cod = String.valueOf(code);
		return getCode(cod, nChars);
	}
	
	public static String getExtract(String str, int maxLength) {
		if (str.length() > maxLength) {
			str = str.substring(0, maxLength);
			int ue = str.lastIndexOf(" ");
			
			System.out.println("ue: " + ue);
			
			if (ue > 0) str = str.substring(0, ue);
			
			return str + " ...";
		}	else return str; 
	}

    public static String getStr4IN(String str) {
        String res = "";
        String[] vals = str.split(",");

        for (int i = 0; i < vals.length; i ++) res += "'" + vals[i] + "', ";

        return res.substring(0, res.length() - 2);
    }
    
    
    public static boolean isNumeric(String cadena){
 	    try {
 	    	Float.parseFloat(cadena);
 	    	return true;
	 
	    } catch (NumberFormatException nfe){
	    	return false;
	    }
    }
    
    public static boolean isFloat(String cadena) {
    	boolean res = false;
    	if (Codes.isNumeric(cadena)) {
    		try {
    			if (Integer.parseInt(cadena) != Float.parseFloat(cadena)) res = true;
    		} catch (NumberFormatException nfe){
    	    }	
    	}	
    	return res;
    }
    
    public static String setFecha(String fecha) {
    	String[] fcs = fecha.split("-");
    	
    	return fcs[2] + "-" + fcs[1] + "-" + fcs[0];
    }
    
    public static String fillZeros(String num) {
        int n = (int)Math.ceil((double)num.length() / 3) * 3 - num.length(), i;

        for (i = 0; i < n; i ++) num = "0" + num;

        return num;
    }
    
    public static String readNumber(float number) {
    	String say = "";
        
        String[] unis = {"", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve"};
        String[] teens = {"diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho",
                          "diecinueve"};
        String[] decis = {"", "", "veinti", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa"};
        String[] centis = {"", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos",
                           "setecientos", "ochocientos", "novecientos"};

    	String[] levels = {"", "mil", "millones"};

    	String[] nms = DFormat.cformat(number, 2).split("[.]");
    	String ent = fillZeros(nms[0]);
    	int dec = Integer.parseInt(nms[1]);

    	String[] nums = ent.split("");

        int n = nums.length;
        //System.out.println("n: " + nums.length);
    	int nl = (int)Math.ceil((double)(nums.length - 1) / 3), l;

    	for (l = 0; l < nl; l ++) {
            int uni = 0, dcn = 0, cen = 0;
            
            if (l * 3 + 3 < n) {
                cen = Integer.parseInt(nums[l * 3 + 1]);

            }   

            if (l * 3 + 2 < n) {
                dcn = Integer.parseInt(nums[l * 3 + 2]);
            }  

            if (l * 3 + 1 < n) {
                uni = Integer.parseInt(nums[l * 3 + 3]);
            }  

            say += " ";

            if (cen == 1 && dcn == 0 && uni == 0) say += "cien";
            else say += centis[cen];

            say += " ";

            if (dcn == 1) {
               say += teens[uni];
            }   else {
                if (uni == 0 && dcn == 2) say += "veinte";
                else say += decis[dcn];
            }


            if (dcn != 1 && uni > 0) {
                if (dcn > 2) say += "i";
                if (nl - l - 1 > 0 && uni == 1) {
                    if (dcn > 1) say += "un";
                }   else say += unis[uni];
            }

            say += " " + levels[nl - l - 1];
    	}
    	
    	say = say.trim();
    	say = say.substring(0, 1).toUpperCase() + say.substring(1) + " y " + dec + "/100 Nuevos Soles";

    	return say;
    }

    public static String md5(Object text) {
        StringBuilder hexString = new StringBuilder();

        try {
            byte[] defaultBytes = text.toString().getBytes();
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(defaultBytes);
            byte[] messageDigest = algorithm.digest();

            for (int i = 0; i < messageDigest.length; i++) {
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Codes.class.getName()).log(Level.SEVERE, null, ex);
        }
        return hexString.toString();
    }
}